package com.benjaminwan.read.free.ui.settings

import android.os.Bundle
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import com.airbnb.mvrx.activityViewModel
import com.airbnb.mvrx.withState
import com.benjaminwan.read.free.base.fragment.BaseFragment
import com.benjaminwan.read.free.R
import com.benjaminwan.read.free.databinding.FragmentAboutBinding
import com.benjaminwan.read.free.databinding.FragmentSettingsBinding
import com.benjaminwan.read.free.utils.setKeepScreenOnEnable

class SettingsFragment : BaseFragment(), View.OnClickListener {

    private val settingsVM by activityViewModel(SettingsViewModel::class)

    private lateinit var binding: FragmentSettingsBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentSettingsBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initViews()
    }

    private fun initViews() {
        binding.keepScreenOnStv.setSwitchCheckedChangeListener { compoundButton, isChecked ->
            requireActivity().setKeepScreenOnEnable(isChecked)
            settingsVM.setKeepScreenOn(isChecked)
        }
        binding.keepScreenOnStv.setOnClickListener(this)
        binding.showGuideStv.setCheckBoxCheckedChangeListener { compoundButton, isChecked ->
            settingsVM.setIsShowGuide(isChecked)
        }
        binding.showGuideStv.setOnClickListener(this)
        binding.vibratorEnableStv.setSwitchCheckedChangeListener { compoundButton, isChecked ->
            settingsVM.setVibratorEnable(isChecked)
        }
        binding.vibratorEnableStv.setOnClickListener(this)
    }

    override fun onClick(view: View?) {
        view ?: return
        when (view.id) {
            R.id.keepScreenOnStv -> {
                withState(settingsVM) {
                    requireActivity().setKeepScreenOnEnable(!it.keepScreenOn)
                    settingsVM.setKeepScreenOn(!it.keepScreenOn)
                }
            }
            R.id.showGuideStv -> {
                withState(settingsVM) {
                    settingsVM.setIsShowGuide(!it.isShowGuide)
                }
            }
            R.id.vibratorEnableStv -> {
                withState(settingsVM) {
                    settingsVM.setVibratorEnable(!it.vibratorEnable)
                }
            }
            else -> {
            }
        }
    }

    override fun invalidate() = withState(settingsVM) { settings ->
        binding.keepScreenOnStv.setSwitchIsChecked(settings.keepScreenOn, true)
        binding.showGuideStv.setCheckBoxChecked(settings.isShowGuide, true)
        binding.vibratorEnableStv.setSwitchIsChecked(settings.vibratorEnable, true)
        Unit
    }
}